package com.yunhe.rentcars.pro.service.client.impl;

import com.yunhe.rentcars.commons.dto.Pageable;
import com.yunhe.rentcars.pro.dao.client.CreditRecordDao;
import com.yunhe.rentcars.pro.entity.client.ClientUser;
import com.yunhe.rentcars.pro.entity.client.Credit;
import com.yunhe.rentcars.pro.service.client.ClientUserService;
import com.yunhe.rentcars.pro.service.client.CreditRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/11/29 0029.
 */
@Service
public class CreditRecordServiceImpl implements CreditRecordService {
    @Autowired
    private CreditRecordDao creditRecordDao;
    @Autowired
    private ClientUserService clientUserService;
    @Override
    public Pageable<Credit> findPage(Integer offset, Integer limit) {

        int count = creditRecordDao.count();
        List<Credit> list = creditRecordDao.findPage(offset, limit);
        Pageable<Credit> pageable=new Pageable();
        pageable.setTotal(count);
        pageable.setRows(list);
        return pageable;
    }

    @Override
    public void save(Credit credit) {
        ClientUser clientUser = clientUserService.find(credit.getuId());
        Integer num = Integer.valueOf(String.valueOf(credit.getCreditChange().charAt(1)));
        if(String.valueOf(credit.getCreditChange().charAt(0)).equals("+")){
            clientUser.setCreditNum(clientUser.getCreditNum()+num);
        }else{
            clientUser.setCreditNum(clientUser.getCreditNum()-num);
        }
        clientUserService.update(clientUser);
        creditRecordDao.save(credit);
    }

    @Override
    public void delBatch(String[] ids) {

    }

    @Override
    public ClientUser find(Long id) {
        return null;
    }


    @Override
    public void update(Credit credit) {

    }

    @Override
    public Pageable<Credit> findSearchList(String mobile, String idCard) {
        List<Credit> searchList = creditRecordDao.findSearchList(mobile, idCard);
        Pageable<Credit> pageable=new Pageable<>();
        pageable.setRows(searchList);
        pageable.setTotal(searchList.size());
        return pageable;
    }


}
